package com.hurr.common.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hurr.common.dao.GenericDao;

/**
 * Generic Dao implementation using {@link HibernateDaoSupport}
 * 
 * @author Fernando Perelli
 * @param <Entity>
 * @param <T>
 */
public abstract class GenericDaoImpl<Entity, T extends Serializable> extends
		HibernateDaoSupport implements GenericDao<Entity, T> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hurr.common.dao.GenericDao#save(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Entity save(Entity entity) {

		return (Entity) super.getHibernateTemplate().get(getClass(),
				(T) super.getHibernateTemplate().save(entity));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hurr.common.dao.GenericDao#update(java.lang.Object)
	 */
	@Override
	public void update(Entity entity) {
		super.getHibernateTemplate().update(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hurr.common.dao.GenericDao#delete(java.lang.Object)
	 */
	@Override
	public void delete(Entity entity) {
		super.getHibernateTemplate().delete(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hurr.common.dao.GenericDao#findById(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Entity findById(T id) {
		return (Entity) super.getHibernateTemplate().load(getClass(), id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hurr.common.dao.GenericDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Entity> findAll() {
		return (List<Entity>) super.getHibernateTemplate().loadAll(getClass());
	}

}
